from .base import Handler
from models.task_queue import TaskQueue
from event_engine.event_type import *
from event_engine.event import Event
from event_engine.engine import EventEngine
import time


class GetTaskHandler(Handler):
    """
    获取任务
    """

    def process(self, event: Event, engine: EventEngine) -> None:
        # 从任务队列取新任务
        for _ in range(10):
            task = TaskQueue.get()
            if task:
                # 有新任务，put 获取代理IP事件
                engine.put(Event(EVENT_GET_PROXY_IP, {"task": task}))
                return
            time.sleep(1)

        # 10秒钟没有新任务，put OVER事件，结束事件引擎
        engine.put(Event(OVER))
